{% extends "base.html" %}
{% load core_tags %}
{% block title %} <title>ActivFlow - Workflow</title> {% endblock %}
{% block breadcrumb %}
<ol class="breadcrumb">
  <li><a href="{% url 'workflows' %}">Home</a></li>
  <li class="active">{{workflow_title}}</li>
</ol>
{% endblock %}
{% block main_content %}
<div class="row">
  <div class="col-md-9 col-md-push-3">
    <blockquote class="blockquote-reverse">
		<h6><strong>{{workflow_title}}</strong></h6>
		<h6><i>{{description}}</i></h6>
	</blockquote>
  </div>
  <div class="col-md-3 col-md-pull-9">
    <a href="{% url 'create' app_title initial request_identifier %}" class="btn btn-primary" role="button"><span class="glyphicon glyphicon glyphicon-edit"></span> Initiate Request</a>
  </div>
</div>
{% if requests %}
<table class="table table-bordered">
	<tr>
		<th><span class="glyphicon glyphicon-menu-hamburger"></span></th>
		<th>ID</th>
		<th>Status</th>
		<th>Requester</th>
		<th>Subject</th>
		<th>Created</th>
		<th>Last Updated</th>
    </tr>
    {% for request in requests %}
	<tr class="active">
		<td>
			<button class="btn btn-default btn-xs" type="button" data-toggle="collapse" data-target="#{{request.code}}">
				View Tasks <span class="badge">{{ request.tasks.count }}</span>
			</button>
		</td>
		<td>{{request.id}}</td>
		<td>{{request.status}}</td>
		<td>{{request.requester.username}}</td>
		<td>{{request.tasks.all.0.activity.subject}}</td>
		<td>{{request.creation_date}}</td>
		<td>{{request.last_updated}}</td>
    </tr>
    {% if request.tasks %}
    <tr id="{{request.code}}" class="collapse">
    	<td colspan="7">
		    <table class="table table-condensed">
				<tr>
					<th>ID</th>
					<th>Activity</th>
					<th>Status</th>
					<th>Assignee</th>
					<th>Date Assigned</th>
					<th>Last Updated</th>
					<th></th>
			    </tr>
			    {% for task in request.tasks.all %}
				<tr>
					{% activity_friendly_name task.activity_ref app_title as friendly_title %}
					<td>{{task.id}}</td>
					<td>{{friendly_title}}</td>
					<td>{{task.status}}</td>
					<td>{{task.assignee.name}}</td>
					<td>{{task.creation_date}}</td>
					<td>{{task.last_updated}}</td>
					{% activity_title task.activity_ref app_title as act_title %}
					<td>
						{% with identifier=task.activity.id|default:None %}
						<a class="btn btn-info btn-xs {% if not task.can_view_activity %} disabled {% endif %}" href="{% url 'view' app_title act_title identifier %}"><span class="glyphicon glyphicon glyphicon-check"></span> View</a>
						<a class="btn btn-success btn-xs {% if not task.can_initiate_activity %} disabled {% endif %}" href="{% url 'create' app_title act_title task.id %}"><span class="glyphicon glyphicon glyphicon-pencil"></span> Initiate</a>
						<a class="btn btn-primary btn-xs {% if not task.can_revise_activity %} disabled {% endif %}" href="{% url 'update' app_title act_title identifier %}"><span class="glyphicon glyphicon glyphicon-edit"></span> Revise</a>
						<form style="display:inline" action="{% url 'rollback' app_title task.id %}" method="POST">
						  {% csrf_token %}
						  <button type="submit" name="rollback" class="btn btn-warning btn-xs {% if not task.can_rollback %} disabled {% endif %}"><span class="glyphicon glyphicon glyphicon-repeat"></span> Rollback</button>
						</form>
						{% endwith %}
					</td>
			    </tr>
			    {% endfor %}
			</table>
		</td>
	</tr>
	{% else %}
	    <p>No requests are available.</p>
	{% endif %}
	{% endfor %}
</table>
{% else %}
    <p>No requests are available.</p>
{% endif %}
{% endblock %}